package com.zksk.portal.service.member.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.common.core.constant.MemberOperatorConstants;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Member;
import com.zksk.mbg.admin.domain.MemberOperator;
import com.zksk.mbg.admin.mapper.MemberMapper;
import com.zksk.mbg.admin.mapper.MemberOperatorMapper;
import com.zksk.portal.domain.vo.BankVo;
import com.zksk.portal.domain.vo.MemberVo;
import com.zksk.portal.service.member.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberOperatorMapper memberOperatorMapper;

    @Override
    public Boolean saveMember(MemberVo memberVo) {
        //保存企业信息
        Member member = memberVo.getMember();
        member.setUpdateBy(SecurityUtils.getUsername());
        int update = memberMapper.updateById(member);
        //保存经办人信息
        MemberOperator memberOperator = memberVo.getMemberOperator();
        memberOperator.setCreateBy(SecurityUtils.getUsername());
        memberOperator.setUpdateBy(SecurityUtils.getUsername());
        memberOperator.setDefaultOp(MemberOperatorConstants.NORMAL);
        QueryWrapper<MemberOperator> queryWrapper = new QueryWrapper<MemberOperator>()
                .eq("member_id",member.getMemberId()).eq("default_op",MemberOperatorConstants.NORMAL);
        List<MemberOperator> memberOperators = memberOperatorMapper.selectList(queryWrapper);
        for (MemberOperator memberOp : memberOperators) {
            memberOp.setDefaultOp(MemberOperatorConstants.INNORMAL);
            memberOperatorMapper.updateById(memberOp);
        }
        int insert = memberOperatorMapper.insert(memberOperator);
        if (update>0&&insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean saveBank(BankVo bankVo) {
        Integer memberId = bankVo.getMemberId();
        Member member = memberMapper.selectById(memberId);
        member.setBankAccount(bankVo.getBankAccount());
        member.setOpenBank(bankVo.getOpenBank());
        member.setOpenBankFile(bankVo.getOpenBankFile());
        member.setUpdateBy(SecurityUtils.getUsername());
        int i = memberMapper.updateById(member);
        if (i>0){
            return true;
        }
        return false;
    }
}

